package com.yunkont.pigcontrol.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.yunkont.pigcontrol.R;
import com.yunkont.pigcontrol.adapter.MainPagerAapter;
import com.yunkont.pigcontrol.app.BaseApplicaiton;
import com.yunkont.pigcontrol.bean.AllPigBean;
import com.yunkont.pigcontrol.fragment.FirstFragment;
import com.yunkont.pigcontrol.fragment.SeconedFragment;
import com.yunkont.pigcontrol.fragment.ThirdFragment;
import com.yunkont.pigcontrol.net.UrlValue;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    private FirstFragment firstFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        firstFragment = new FirstFragment();
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(firstFragment);
        fragments.add(new SeconedFragment());
        fragments.add(new ThirdFragment());
//        fragments.add(new FourthFragment());

        FragmentPagerAdapter fragmentPagerAdapter = new MainPagerAapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(fragmentPagerAdapter);
        tablayout.setupWithViewPager(viewpager);
        viewpager.setOffscreenPageLimit(3);
        getAllPigs();
    }

    private void getAllPigs() {
        OkHttpUtils.get().url(UrlValue.pig)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(MainActivity.this, "网络错误：" + e.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("getAllPigs", "onResponse: "+response );
                try {
                    final AllPigBean allPigBean = JSON.parseObject(response, AllPigBean.class);
                    viewpager.postDelayed(new Runnable(){
                        @Override
                        public void run() {
                            firstFragment.getPigById(allPigBean.get_embedded().getPigConfigs().get(0).getPid());
                        }
                    },500);
                    BaseApplicaiton.getInstance().setAllPigBean(JSON.parseObject(response, AllPigBean.class));
                    BaseApplicaiton.getInstance().setCurrentPid(allPigBean.get_embedded().getPigConfigs().get(0).getPid());
                    BaseApplicaiton.getInstance().setLinkUrl(allPigBean.get_embedded().getPigConfigs().get(0).get_links().getSelf().getHref());
//                    BaseApplicaiton.getInstance().setCurrentPigBean(allPigBean.get_embedded().getPigConfigs().get(0));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
